<?php

namespace App\Admin\Controllers\Common;

use App\Models\CommonAdsense;
use App\Models\CommonAdsenseMedia;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class AdsenseMediaController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '广告媒介信息';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CommonAdsenseMedia());

        $grid->column('id', ___('table.zy_common_adsense_media.Id'));
        $grid->column('adsense_id', ___('table.zy_common_adsense_media.Adsense id'));
        $grid->column('media', ___('table.zy_common_adsense_media.Media'));
        $grid->column('type', ___('table.zy_common_adsense_media.Type'));
        $grid->column('create_time', ___('table.zy_common_adsense_media.Create time'));
        $grid->column('update_time', ___('table.zy_common_adsense_media.Update time'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CommonAdsenseMedia::findOrFail($id));

        $show->field('id', ___('table.zy_common_adsense_media.Id'));
        $show->field('adsense_id', ___('table.zy_common_adsense_media.Adsense id'));
        $show->field('media', ___('table.zy_common_adsense_media.Media'));
        $show->field('type', ___('table.zy_common_adsense_media.Type'));
        $show->field('create_time', ___('table.zy_common_adsense_media.Create time'));
        $show->field('update_time', ___('table.zy_common_adsense_media.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CommonAdsenseMedia());

        $form->select('adsense_id', ___('table.zy_common_adsense_media.Adsense id'))->options(CommonAdsense::all()->pluck('alias', 'id'));
        $form->radio('type', ___('table.zy_common_adsense_media.Type'))
            ->options([0 => '图片', 1=> '视频'])->default(1)
            ->when(0, function (Form $form){
                $this->imageMedia($form, 'media',  ___('table.zy_common_adsense_media.Media'));
            })
            ->when(1, function (Form $form){
                $this->urlMedia($form, 'media',  ___('table.zy_common_adsense_media.Media'));
            });
        return $form;
    }

    protected function imageMedia($form, $column, $label){
        // $form->image($column, $label);
        $form->multipleImage($column, $label)->removable();

        // 修改图片上传路径和文件名
        // $form->image($column[, $label])->move($dir, $name);

        // 剪裁图片
        // $form->image($column[, $label])->crop(int $width, int $height, [int $x, int $y]);

        // 加水印
        // $form->image($column[, $label])->insert($watermark, 'center');

        // 添加图片删除按钮
        // $form->image($column[, $label])->removable();

        // 删除数据时保留图片
        // $form->image($column[, $label])->retainable();

        // 增加一个下载按钮，可点击下载
        // $form->image($column[, $label])->downloadable();
    }

    protected function urlMedia($form, $column, $label){
        $form->url($column, $label);
    }
}
